/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package jpa.entities;

import java.io.Serializable;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;
import statements.beans.CCStatementCSV;

/**
 *
 * @author bsmith
 */
@Entity
@Table(name = "statement")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Statement.findAll", query = "SELECT s FROM Statement s"),
    @NamedQuery(name = "Statement.findByDate", query = "SELECT s FROM Statement s WHERE s.date = :date"),
    @NamedQuery(name = "Statement.findById", query = "SELECT s FROM Statement s WHERE s.id = :id"),
    @NamedQuery(name = "Statement.findByType", query = "SELECT s FROM Statement s WHERE s.type = :type"),
    @NamedQuery(name = "Statement.findByAmount", query = "SELECT s FROM Statement s WHERE s.amount = :amount"),
    @NamedQuery(name = "Statement.findByBalance", query = "SELECT s FROM Statement s WHERE s.balance = :balance"),
    @NamedQuery(name = "Statement.findByMerchant", query = "SELECT s FROM Statement s WHERE s.merchant = :merchant")})
public class Statement implements Serializable {

    private static final long serialVersionUID = 1L;
    @Basic(optional = false)
    @NotNull
    @Column(name = "date")
    @Temporal(TemporalType.TIMESTAMP)
    private Date date;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    //@NotNull
    @Column(name = "id")
    private Long id;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 128)
    @Column(name = "type")
    private String type;    
    @Basic(optional = false)
    @NotNull
    @Column(name = "amount")
    private int amount;
    @Basic(optional = false)
    @NotNull
    @Column(name = "balance")
    private int balance;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 128)
    @Column(name = "merchant")
    private String merchant;

    public Statement() {
    }

    public Statement(Long id) {
        this.id = id;
    }

    public Statement(Long id, Date date, String type, int amount, int balance, String merchant) {
        this.id = id;
        this.date = date;
        this.type = type;
        this.amount = amount;
        this.balance = balance;
        this.merchant = merchant;
    }

    public Statement(CCStatementCSV ccStatementCSV) throws ParseException {
        setDate(ccStatementCSV.getDate());
        setType(ccStatementCSV.getType());
        setAmount(ccStatementCSV.getAmount());
        setBalance(ccStatementCSV.getBalance());
        setMerchant(ccStatementCSV.getMerchant());
    }

    public Date getDate() {
        return date;
    }

    public void setDate(Date date) {
        this.date = date;
    }

    public void setDate(String datestr) throws ParseException {
        DateFormat dateFormat = new SimpleDateFormat("dd/MM/yy");

        this.date = dateFormat.parse(datestr);

    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public int getAmount() {
        return amount;
    }

    public void setAmount(int amount) {
        this.amount = amount;
    }

    public void setAmount(String amountStr) {
        Float floatAmt = Float.parseFloat(amountStr);
        floatAmt *= 100;    // convert to cents
        this.amount = floatAmt.intValue();
    }

    public int getBalance() {
        return balance;
    }

    public void setBalance(int balance) {
        this.balance = balance;
    }

    public void setBalance(String balanceStr) {
        Float floatAmt = Float.parseFloat(balanceStr);
        floatAmt *= 100;    // convert to cents
        this.balance = floatAmt.intValue();
    }

    public String getMerchant() {
        return merchant;
    }

    public void setMerchant(String merchant) {
        this.merchant = merchant;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (id != null ? id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object object) {
        // TODO: Warning - this method won't work in the case the id fields are not set
        if (!(object instanceof Statement)) {
            return false;
        }
        Statement other = (Statement) object;
        if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "jpa.entities.Statement[ id=" + id + " ]";
    }
}
